#include <Arduino.h>
#include <LiquidCrystal.h>
#include <EtherCard.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define SEND_INTERVAL  30000
#define TIMEOUT        5000
#define ONE_WIRE_BUS   2
#define STATUS_IDLE    0
#define STATUS_SENT    1

// Inisialisasi koneksi LCD
LiquidCrystal lcd(9, 8, 7, 6, 5, 4, 3);

static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
byte Ethernet::buffer[700];

char website[] PROGMEM = "192.168.42.50";
char password[] PROGMEM = "s1b3d390n9";

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

unsigned long previousMillis = 0;
static byte session_id;
byte actual_status;


void sendTemperature()
{
  sensors.requestTemperatures();
  float float_temp = sensors.getTempCByIndex(0);
  char string_temp[7];
  dtostrf(float_temp, 4, 2, string_temp);

  Stash stash;
  byte sd = stash.create();
  stash.print(string_temp);
  stash.save();

  Stash::prepare(PSTR("GET /suhu/saveTemp.php?temp=$H&pwd=$F HTTP/1.0" "\r\n" "Host: $F" "\r\n" "\r\n"), sd, password, website);
  session_id = ether.tcpSend();
  lcd.clear();
  lcd.setCursor(0,0); lcd.print("Suhu "); lcd.print(string_temp);lcd.print(char(223));lcd.print("C");
  lcd.setCursor(0,1); lcd.print("Dikirim..");
  actual_status = STATUS_SENT;
}

void checkResponse()
{

  const char* reply = ether.tcpReply(session_id);
  if(reply > 0)
    {
        if(strstr(reply, "KO - ") != 0) lcd.print((strstr(reply, "KO - ")));
        else lcd.print("OK");
        actual_status = STATUS_IDLE;
    }

}

void setup ()
{
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Web Thermometer ");
  lcd.setCursor(0,1); lcd.print("Oleh MD4N1 2013");
  delay(2000);
  lcd.clear();
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    {
    }

  if (!ether.dhcpSetup())
    {
        lcd.setCursor(0,0);
        lcd.print("DHCP: gagal");
        while(1);
    }
    else
    {
        lcd.setCursor(0,0);
        lcd.print("DHCP: sukses bos");
    }


  if (!ether.dnsLookup(website))
    {
      lcd.setCursor(0,1);
      lcd.print("IP Web: gagal bos");
      while(1);
    } else
    {
       lcd.setCursor(0,1);
       lcd.print("IP  : sukses bos");
    }
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("S:");
  for (byte thisByte = 0; thisByte < 4; thisByte++)
     {
        lcd.print(ether.myip[thisByte], DEC);
        if (thisByte <3) lcd.print(".");
     }

  lcd.setCursor(0,1);
  lcd.print("D:");
  for (byte thisByte = 0; thisByte < 4; thisByte++)
     {
        lcd.print(ether.hisip[thisByte], DEC);
        if (thisByte <3) lcd.print(".");
     }
}


void loop()
{
  ether.packetLoop(ether.packetReceive());
  unsigned long currentMillis = millis();

  switch(actual_status)
  {
    case STATUS_IDLE:
      if(currentMillis - previousMillis > SEND_INTERVAL)
        {
            previousMillis = currentMillis;
            sendTemperature();
        }
      break;
    case STATUS_SENT:
      if(currentMillis - previousMillis > TIMEOUT)
        {
            previousMillis = currentMillis;
            actual_status = STATUS_IDLE;
      }
        checkResponse();
  }
}

